/* Author: DO HOANG HAI
 * Main.java
 *
 * Created on June 4, 2006, 8:58 PM
 *
 * Description: This project demonstrates how to use selection sort algorithm to sort an 1-dimension array
 */

package selection;
import java.util.Scanner;
/**
 *
 * @author hai
 */
public class Main {
    
    
    private static Scanner reader=new Scanner(System.in);
    private static int array[]=new int[20];
        
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        inputData();
        
        sortData();
        displayResult();
        
    }
    public static void inputData(){
        int ar[]=new int[20];
        System.out.println("   Enter values to be sorted, finish by -1: ");
        int index=0;
        int value=reader.nextInt();
        while (value!=-1){
            ar[index]=value;
            value=reader.nextInt();     
            index++;
        }
        array= new int[index];
        for (int i=0; i<index; i++){
            array[i]=ar[i];
        }        
        
    }
    //------------
     public static void displayResult(){
        for (int index=0; index<array.length; index++)
            System.out.print("  "+ array[index]);
    }
   //----------------
    public static void sortData(){
        for (int i=0; i<array.length-1; i++){
            for (int j=array.length-1; j>i; j--){
                if (array[j]<array[j-1]){
                    //swap 2 values
                    int temp = array[j-1];
                    array[j - 1] = array[j];
                    array[j]= temp;
                    
                }
                    
            }
            
        }
    }   
}
